﻿using System.Collections.Generic;
using System.Linq;
using KSPCapability;
using UnityEngine;

public class CapabilityBooster : Part
{
	private static Krakensbane _krakensbane;
	public static Krakensbane Krakensbane
	{
		get { return _krakensbane ?? (_krakensbane = (Krakensbane)UnityEngine.Object.FindObjectOfType(typeof(Krakensbane))); }
	}
	
	public List<Range> AvaliableOptions
	{
		get { return _avaliableOptions; }
	}

	protected Rect WindowPos;

	protected List<KSPCapability.Capability> Capabilities;

	private void WindowGui(int windowId)
	{
		GUIStyle mySty = new GUIStyle(GUI.skin.button);
		mySty.normal.textColor = mySty.focused.textColor = Color.white;
		mySty.hover.textColor = mySty.active.textColor = Color.yellow;
		mySty.onNormal.textColor = mySty.onFocused.textColor = mySty.onHover.textColor = mySty.onActive.textColor = Color.green;
		mySty.padding = new RectOffset(8, 8, 8, 8);

		GUILayout.BeginVertical();

		if (FlightGlobals.ActiveVessel.packed == false)
		{
			GUILayout.BeginHorizontal();
			
			if(GUILayout.Button("Orbits"))
				_orbitGui = new OrbitGUI(this);

			if (GUILayout.Button("Bases"))
				_orbitGui = new BaseGUI(this);

			GUILayout.EndHorizontal();

			if(_orbitGui != null) _orbitGui.Render();
		}
		else
		{
			GUILayout.Label("Awaiting vessel ready");
		}

		GUILayout.EndVertical();

		//DragWindow makes the window draggable. The Rect specifies which part of the window it can by dragged by, and is 
		//clipped to the actual boundary of the window. You can also pass no argument at all and then the window can by
		//dragged by any part of it. Make sure the DragWindow command is AFTER all your other GUI input stuff, or else
		//it may "cover up" your controls and make them stop responding to the mouse.
		GUI.DragWindow(new Rect(0, 0, 10000, 20));
	}

	private void DrawGui()
	{
		GUI.skin = HighLogic.Skin;
		WindowPos = GUILayout.Window(9982, WindowPos, WindowGui, "Capability Booster", GUILayout.MinWidth(150));
	}

	public class Range
	{
		public string Body { get; set; }
		public Capability Min { get; set; }
		public Capability Max { get; set; }
	}

	protected override void onFlightStart()  //Called when vessel is placed on the launchpad
	{
		base.onFlightStart();
		var caps = KSPCapability.CapabilityPersistance.Instance.GetCapabilities();

		_avaliableOptions = caps
			.Where(c => c.Mass >= FlightGlobals.ActiveVessel.GetTotalMass())
			.GroupBy(c => c.Body).Select(g => new CapabilityBooster.Range
				                                  {
					                                  Body = g.Key,
													  Min = new Capability()
														{
															Altitude = g.Min(e => e.Altitude),
															Inclination = g.Min(e => e.Inclination),
															LAN = -180.0
														},
														Max = new Capability()
														{
															Altitude = g.Max(e => e.Altitude),
															Inclination = g.Max(e => e.Inclination),
															LAN = 180.0
														}
				                                  })
			.ToList();

		RenderingManager.AddToPostDrawQueue(3, new Callback(DrawGui));//start the GUI
	}


	protected override void onPartStart()
	{
		if ((WindowPos.x == 0) && (WindowPos.y == 0))//windowPos is used to position the GUI window, lets set it in the center of the screen
		{
			WindowPos = new Rect(Screen.width / 2, Screen.height / 2, 10, 10);
		}
	}

	private void RemoveGui()
	{
		RenderingManager.RemoveFromPostDrawQueue(3, new Callback(DrawGui)); //close the GUI
	}

	protected override void onPartDetach()
	{
		base.onPartDetach();

		RemoveGui();
	}

	protected override void onPartDestroy()
	{
		base.onPartDestroy();

		RemoveGui();
	}

	protected override void onPartFixedUpdate()
	{
		base.onPartFixedUpdate();

		_orbitGui.FixedUpdate();
	}
	
	private List<Range> _avaliableOptions;
	private IWarpGUI _orbitGui =  null;

	public CapabilityBooster()
	{
	}
}